﻿#include "FrameRangeAnimator.h"

FrameRangeAnimator::FrameRangeAnimator (void): 
	 	sprite((Sprite*) 0), 
		anim((FrameRangeAnimation*) 0), currFrame(0xFF) {}

void FrameRangeAnimator::Start(Sprite* s, FrameRangeAnimation* a, timestamp_t t) {
	sprite = s;
	anim = a;
	lastTime = t;
	state = ANIMATOR_RUNNING;
	anim->SetFlipH(sprite->GetFlipH());
	anim->SetFlipV(sprite->GetFlipV());
	sprite->SetFrame(currFrame = anim->GetStartFrame());
}

void FrameRangeAnimator::Progress (timestamp_t currTime) {

	while (currTime > lastTime && currTime - lastTime >= anim->GetDelay()) {
	    if (currFrame == anim->GetEndFrame())
			currFrame = anim->GetStartFrame();
	    else
			++currFrame;

	    sprite->Move(anim->GetDx(), anim->GetDy());
		sprite->SetFlipH(anim->GetFlipH());
		sprite->SetFlipV(anim->GetFlipV());
	    sprite->SetFrame(currFrame);
	    lastTime += anim->GetDelay();

	    if (currFrame == anim->GetEndFrame() && !anim->GetContinuous()) {
			state = ANIMATOR_FINISHED;
			NotifyStopped();
			return;
	    }
	}
}

